import com.gomefinance.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/16.
 */
public class TestTheCode {

    public static void  main(String[] args){
        String ep = "ZJK|0001|2/2|-HK|0002|1/1|10/10";
        for (int i = 0 ; i <3 ; i++) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
                    ep = func(new Date(),ep);

//                }
//           }).start();
         }

         ep = func(DateUtils.parseDate(DateUtils.getAfterDate(new Date(),"add")),ep);
        for (int i = 0 ; i <31 ; i++) {
            ep = func(new Date(),ep);
        }
    }


    public static  String func(Date date,String ep){
        String returnCode = null;// 返回结果
        StringBuffer sb = new StringBuffer();

        try {
            Map<String, Object> map =new HashMap<String,Object>();
            map.put("item_name",ep);
            map.put("update_time",date);
            map.put("dic_id",1);
            Integer dId = Integer.valueOf(map.get("dic_id").toString());// 本条字典ID，方便以下更新
            if (map.get("item_name") == null) {
                System.out.println(" item_name == null");
            } else if ("0".equals(map.get("item_name").toString().trim())) {
                System.out.println("\"0\".equals(map.get(\"item_name\").toString().trim()");
            } else {
                String target = map.get("item_name").toString();
                String[] orgStrs = null;
                try {
                    orgStrs = target.split("-");
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("bankCode临时规则：规则表达式解析失败！其中进件信息为" + 1 + "，异常为：" + e);
                    System.out.println("张家口");
                }
                if (orgStrs == null || orgStrs.length <= 0) {
                    System.out.println("bankCode临时规则：规则表达式不可用！其中进件信息为" + 1);
                    System.out.println("张家口");
                }
                // 判断是否是同一天
                Date cDate = new Date();
                Date oDate = null;
                if (map.get("update_time") != null) {
                    oDate = (Date) map.get("update_time");
                } else {
                    oDate = cDate;
                }
                int d = DateUtils.daysOfTwo(cDate, oDate);
                // 主体业务
                for (int i = 0; i < orgStrs.length; i++) {
                    boolean isOk = false;// 是否OK
                    boolean consumeFlag = false;// 是否被消费
                    String orgStr = orgStrs[i];
                    String[] strs = null;
                    try {
                        strs = orgStr.split("\\|");
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("bankCode临时规则：机构表达式解析失败！其中进件信息为" + 1 + "，异常为：" + e);
                        System.out.println("张家口");
                    }
                    if (strs == null || strs.length <= 0) {
                        System.out.println("bankCode临时规则：机构表达式不可用！其中进件信息为" + 1);
                        System.out.println("张家口");
                    }
                    String orgName = strs[0];
                    String orgCode = strs[1];
                    String durable1 = strs[2];// 权重耐久表达式
                    String durable2 = null;
                    if (strs.length > 3)
                        durable2 = strs[3];// 笔数耐久表达式
                    Integer durable_num1 = null;// 权重耐久
                    Integer durable_num2 = null;// 笔数耐久
                    Integer durable_count1 = null;// 权重耐久
                    Integer durable_count2 = null;// 笔数耐久
                    if (StringUtils.isNotEmpty(durable1)) {
                        durable_num1 = Integer.valueOf(durable1.split("/")[0]);// 权重耐久
                        durable_count1 = Integer.valueOf(durable1.split("/")[1]);// 权重耐久
                    }
                    if (StringUtils.isNotEmpty(durable2)) {
                        durable_num2 = Integer.valueOf(durable2.split("/")[0]);// 笔数耐久
                        durable_count2 = Integer.valueOf(durable2.split("/")[1]);// 笔数耐久
                    }
                    //判断是否OK
                    if (StringUtils.isNotEmpty(durable2)) {
                        if (durable_count1 - durable_num1 < durable_count1 && durable_count1 - durable_num1 >= 0
                                && durable_count2 - durable_num2 < durable_count2
                                && durable_count2 - durable_num2 >= 0) {
                            isOk = true;
                        }
                    } else {
                        if (durable_count1 - durable_num1 < durable_count1 && durable_count1 - durable_num1 >= 0) {
                            isOk = true;
                        }
                    }
                    // 为returnCode赋值
                    if (isOk && StringUtils.isEmpty(returnCode)) {
                        returnCode = orgCode;
                        consumeFlag = true;
                    }
						/* 重组表达式 start */
                    // 如果不同天
                    if (d != 0) {
                        // 被消费 耐久度-1
                        if (consumeFlag) {
                            durable_num1 = durable_count1 - 1;// 权重
                            if (StringUtils.isNotEmpty(durable2)) {
                                durable_num2 = durable_num2 - 1;// 笔数
                            }
                        }
                        // 没有被消费 重置最大耐久度
                        else {
                            durable_num1 = durable_count1;// 权重
                            if (StringUtils.isNotEmpty(durable2)) {
                                durable_num2 = durable_count2;// 笔数
                            }
                        }
                    }
                    // 同天
                    else {
                        // 被消费 耐久度-1
                        if (consumeFlag) {
                            durable_num1 = durable_num1 - 1;// 权重
                            if (StringUtils.isNotEmpty(durable2)) {
                                durable_num2 = durable_num2 - 1;// 笔数
                            }
                        }
                        // 没有被消费
                        else {
                            // 如果权重耐久为0 则置为最大值
                            if (durable_num1 == 0) {
                                durable_num1 = durable_count1;// 权重
                            } else {
                                durable_num1 = durable_num1;// 权重
                            }
                            if (StringUtils.isNotEmpty(durable2)) {
                                durable_num2 = durable_num2;// 笔数
                            }
                        }
                    }
                    orgStrs[i] = orgName + "|" + orgCode + "|" + durable_num1 + "/" + durable_count1 + "|"
                            + (durable2 != null ? durable_num2 + "/" + durable_count2 : "");// 重组机构规则表达式
						/* 重组表达式 end */
                }
                String item_name = "";
                for (int i = 0; i < orgStrs.length; i++) {
                    String orgStr = orgStrs[i];
                    item_name += orgStr;
                    item_name += "-";
                }
                item_name = item_name.substring(0, item_name.length() - 1);
                System.out.println(">>>>>>>>>>>>>>>>>>>" + item_name + "_" + returnCode + "<<<<<<<<<<<<<<<<<<<<<<");
                //   String sql = "update ECMS_SYSTEM_DICTIONARY set item_name = ?,update_time = ? where dic_id =  ?";
                //   dictionaryDao.updateBySql(sql, item_name, cDate, dId);
                ep = item_name;
                map.put("item_name",ep);

                System.out.println("returnCode:"+returnCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("bankCode临时规则：规则运算失败！其中进件信息为" + 1 + "，异常为：" + e);
        }
        if (StringUtils.isEmpty(returnCode)) {
            System.out.println("returnCode :"+returnCode);
            //System.out.println("张家口");
        }else{
            System.out.println(returnCode);
        }
        return  ep;
    }
}// endclass
